Vue.component('dept-company', {
  template: `
    <el-autocomplete
            v-if="!dataId"
            v-model="form.issued_name"
            class="inline-input"
            :fetch-suggestions="companyQuerySearch"
            placeholder="发行主体"
            :trigger-on-focus="false"
            :maxlength="100"
            @select="companyHandleSelect"
        >
            <template v-if="dataId" slot="append" @click="companyHandleSelect">
                <span>查看标的公司信息</span>
            </template>
        </el-autocomplete>
        <el-input v-else v-model="form.issued_name" readonly show-word-limit :maxlength="100" />
  `,
  props: {
    form: {
      type: Object,
      require: true
    }
  },
  data() {
    return {
      tycParam: {},
      dataId: undefined,
      queryParams: {},
    }
  },
  created() {
    this.dataId = window.parent.dataId
  },
  mounted() {
    console.log(this.form)
  },
  watch: {
    'form': {
      handler(val) {
        this.$nextTick(() => {
          if (this.form.id) {
            axios({
              url: window.parent.baseUrl + '/company/companyBase/' + val.issued,
              headers: {
                'Content-Type':'application/json;charset=utf-8',
                'Authorization': window.parent.token
              },
              method: 'get'
            }).then(res => {
              let _res = res.data
              if (_res.code === 200) {
                this.form.issued_name = _res.data.companyName
              }
            })
          }
        })
      },
      deep: true
    }
  },
  methods: {
    companyQuerySearch(queryString, cb) {
      axios({
        url: window.parent.baseUrl + '/company/companyBase/companyListByName?companyName=' + queryString,
        headers: {
          'Content-Type':'application/json;charset=utf-8',
          'Authorization': window.parent.token
        },
        method: 'get'
      }).then(res => {
        let _res = res.data
        if (_res.code === 200) {
          cb(_res.data)
        }
      })
    },
    // 标的公司选择后事件
    companyHandleSelect(item) {
      this.form.issued_name = item.name
    }
  }
})
